package 第一题;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class StudentBusiness {
    private static List<Student> studentList = new ArrayList<>();

    public static void ReadXMLFile()throws Exception{
        //创建SAXReader对象
        SAXReader reader = new SAXReader();
        //通过reader对象加载xml文件获取document对象
        Document document =reader.read(new File("F:\\Java高级API\\网络编程\\src\\main\\java高级特性综合作业\\MyDoc\\Students.xml"));
        //获取所有XML中student标签
        List<Element> elementList=document.getRootElement().elements("student");

        //将获取到的文本信息放到Student对象中去
        for (Element e: elementList) {
            Student student = new Student();
            student.setName(e.elementText("name"));
            student.setAge(e.elementText("age"));
            student.setGender(e.elementText("gender"));
            student.setClassName(e.elementText("className"));

            studentList.add(student);
        }

        /*for (Student s: studentList) {
            System.out.println(s.toString());
        }*/
    }
    public  static List<Student> showAllStudent()throws Exception{
        studentList.clear();
        ReadXMLFile();
        /*System.out.println("姓名\t年龄\t性别\t所属班级");
        for (Student s: studentList) {
            System.out.println(s.getName()+"\t"+s.getAge()+"\t\t"+s.getGender()+"\t\t"+s.getClassName());
        }*/
        return studentList;
    }
    public static List<Student>  findByName(String name)throws Exception{

        List<Student> list = new ArrayList<>();

         ReadXMLFile();
        //System.out.println("姓名\t年龄\t性别\t所属班级");
        boolean falg=false;
        for (Student s: studentList) {
            if (s.getName().equals(name)){
                /*System.out.println("姓名\t年龄\t性别\t所属班级");
                System.out.println(s.getName()+"\t"+s.getAge()+"\t\t"+s.getGender()+"\t\t"+s.getClassName());*/
                falg=true;
                list.add(s);
                return list;
            }
        }
        if (!falg){
            //System.out.println("你查找的人不存在喔");
            return list;
        }
       return list;
    }
    /*public static void main(String[] args)throws Exception {
        //ReadXMLFile();
        //showAllStudent();
        findByName("刘磊");
    }*/
}
